//
//  Configuration.m
//

#import "Configuration.h"

#import "LoginViewController.h"

#import <UIViewControllerCJHelper.h>

#import <UIKit/UIKit.h>

@implementation Configuration

///获取当前视图控制器
+(UIViewController *)getCurrentVC{
    return [UIViewControllerCJHelper findCurrentShowingViewController];
}

///是否是第一次进入app
+(BOOL)isFirstEnterApp{
    BOOL first = [[[NSUserDefaults standardUserDefaults] objectForKey:@"firstEnterApp"] boolValue];
    return first;
}


//计算文字宽度 单行
+(float)getLabelWidthWithText:(NSString *)textString withFont:(UIFont *)font{
    CGSize size = [textString sizeWithAttributes:@{NSFontAttributeName:font}];
    return size.width;
}

//计算文字高度 多行
+(float)getLabelHeightWithText:(NSString *)textString withFont:(UIFont *)font withLBWidth:(float)width{
    CGSize size = [textString boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return size.height;
}


//判断登录状态 如果未登录 就去登录
+(void)judgeLoginStatus{
    if ([Configuration getLoginStatus] == NO) {
        [Configuration loginClick];
        return;
    }
}

//去登陆
+(void)loginClick{
    LoginViewController *vc = [[LoginViewController alloc] init];
    vc.modalPresentationStyle = 0;
    [[Configuration getCurrentVC] presentViewController:vc animated:YES completion:nil];
}

//获取当前登录状态
+(BOOL)getLoginStatus{
    if ([UserInfomationManager shareManager].loginStatus == NO) {
        return NO;
    }
    if ([UserInfomationManager shareManager].token.length <= 0) {
        return NO;
    }
    if ([[UserInfomationManager shareManager].token isEmpty] == YES) {
        return NO;
    }
    return YES;
}


//数组转json字符串
+(NSString *)arrayToJSONString:(NSArray *)array{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

+(NSString *)getPiceWithPrice:(float)price{
    if (price > 0) {
        return [NSString stringWithFormat:@"¥%.02f",price];
    }
    return @"";
}

+(NSString *)getTBPriceWithTBPrice:(NSInteger)tbPrice{
    if (tbPrice > 0) {
        return [NSString stringWithFormat:@"%ldTB",tbPrice];
    }
    return @"";
}

+(NSString *)getTotalPriceWithPrice:(float)price withTbPrice:(NSInteger)tbPrice{
    NSString *priceStr = [Configuration getPiceWithPrice:price];
    NSString *tbPriceStr = [Configuration getTBPriceWithTBPrice:tbPrice];
    
    if ([priceStr isEmpty] == NO && priceStr.length>0 && [tbPriceStr isEmpty] == NO && tbPriceStr.length>0) {
        return [NSString stringWithFormat:@"%@+%@",priceStr,tbPriceStr];
    }
    if ([priceStr isEmpty] == NO && priceStr.length>0) {
        return priceStr;
    }
    if ([tbPriceStr isEmpty] == NO && tbPriceStr.length>0) {
        return tbPriceStr;
    }
    return @"";
}

//复制到剪切板
+(void)pasteWithString:(NSString *)pasteStr{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = pasteStr;
    [[XNProgressHUD shared] showSuccessWithTitle:@"已成功复制到剪切板!"];
}


@end
